<?php
/*************************************************************************************
 * geoipquery.php
 * -----------
 * phpGMapTrips 
 * 
 * This php application allows you to easily record your travel destinations 
 * and display them with Google maps.
 * 
 * Author: Stephane Coste (lescoste@gmail.com)
 * Copyright: (c) 2006 Stephane Coste (http://lescoste.net/)
 * Release Version: 0.1
 * CVS Revision Version: $Revision: 1.8 $
 * Date Started: 2006/02/19
 * Last Modified: $Date: 2007/03/14 14:32:43 $
 *
 *************************************************************************************
 *
 *     This file is part of phpGMapTrips.
 *
 *   phpGMapTrips is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   phpGMapTrips is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with phpGMapTrips; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ************************************************************************************/
?>
<?php
include_once("utils.php");
include_once("config.php");
include_once("geocoderquery.php");

if (empty($visitorIp)) {
	$visitorIp = $_SERVER["REMOTE_ADDR"];
}
$pos = strpos($visitorIp, "192");
if ($pos===false) {
} else if ($pos==0) {
	// use google ip if ip starts with 192
	$visitorIp = "64.208.49.61";
}
echo "<!-- ".$visitorIp;

$f_files = "visitors";
$editTags ="";
$address ="";
$editLat ="";
$editLng ="";


function curl_string ($url,$user_agent){
	$ch = curl_init();
	curl_setopt ($ch, CURLOPT_URL, $url);
	curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
	curl_setopt ($ch, CURLOPT_HEADER, 1);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
	$result = curl_exec ($ch);
	curl_close($ch);
	return $result;
}

function getGeoIp($visitorIp) {
	global $editTags, $address, $editLat, $editLng, $gmapKey;

	$callgeoCoder = false;

	$user_agent = "Mozilla/4.0";
	$pageContents = curl_string("http://api.hostip.info/get_html.php?position=true&ip=".$visitorIp, $user_agent);
	/*
Country: FRANCE (FR)
City: Paris
Latitude: 48.8
Longitude: 2.33333
	 */
	$re = "/Country: (.*)\(/";
	preg_match ( $re, $pageContents, &$matchArray);
	$country = trim(ucwords(strtolower($matchArray[1])));
	$country = replace_accents($country);
	$editTags = $country;
	$geocoderAddress = $country;

	$pos1 = php4_stripos($country, "Unknown country");
	if ($pos1 === false) {
	} else {
		$editTags == "Unknown Country";
		$geocoderAddress = "";
	}

	$re = "/City: (.*)/";
	preg_match ( $re, $pageContents, &$matchArray);
	$address = $matchArray[1];
	$address = replace_accents($address);
	$pos2 = php4_stripos($address, "Unknown City");
	if ($pos2 === false) {
		//      echo "The string '$findme' was not found in the string '$mystring1'";
		$geocoderAddress = $address . " ".$geocoderAddress;
	} else {
		$callgeoCoder = true;
		$address == "Unknown City";
	}
	$address .= " ".$editTags;


	$re = "/Latitude: (.*)/";
	preg_match ( $re, $pageContents, &$matchArray);
	$editLat = $matchArray[1];
	if (count($matchArray) == 0 || empty($editLat)) {
		$callgeoCoder = true;
		$editLat = "0";
	} else {
		$callgeoCoder = false;
	}

	$re = "/Longitude: (.*)/";
	preg_match ( $re, $pageContents, &$matchArray);
	$editLng = $matchArray[1];
	if (count($matchArray) == 0 || empty($editLng)) {
		$callgeoCoder = true;
		$editLng = "0";
	} else {
		$callgeoCoder = false;
	}

	if ($callgeoCoder) {
		$gc = new GeocoderCaller( $gmapKey, $geocoderAddress);
		$gc->callGoogle();
		$editLat = $gc->getLat();
		$editLng = $gc->getLong();
	}

	$result = array();
	$result["lat"] = $editLat;
	$result["lng"] = $editLng;
	$result["tag"] = $editTags;
	$result["address"] = $address;
	return $result;
}

include_once("dbaccess.php");
$pointarray = searchPoints($visitorIp);
//print_r($pointarray);
if (empty($pointarray[$visitorIp])) {
	echo " query hostip ";
	$result = getGeoIp($visitorIp);
	$editLat = $result["lat"];
	$editLng = $result["lng"];
	$editTags = $result["tag"];
	$address = $result["address"];
} else {
	echo " found in cache ";
	$editTags =$pointarray[$visitorIp]["trips"];
	$address =$pointarray[$visitorIp]["address"];
	$editLat =$pointarray[$visitorIp]["lat"];
	$editLng =$pointarray[$visitorIp]["long"];
}

$editName = $visitorIp;
$editNewname = $visitorIp;

echo $visitorIp." ".$address." ".$editLat. " ". $editLng. " -->";

$editUrl = urlencode("geoip.php?ip=".$visitorIp);
if (empty($editTags)) {
	$editTags = "Unknown";
}

$editMoreinfo = array();
$editMoreinfo["date"] = date('d-m-Y H:i:s');
$editMoreinfo["address"] = $address;
$editMoreinfo["referer"] = urlencode($_GET["referer"]);

?>
